package com.sjf.JavaNIO.channel;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * FileChannel通道之间的数据传输
 *
 * @author <huangtang>
 * @since 2022/4/13 17:05
 **/
public class FileChannelDemo3 {

    /**
     * transferFrom()方法
     *
     * @param args
     */
    public static void main ( String[] args ) throws Exception {
        //创建两个FileChannel
        RandomAccessFile aFile = new RandomAccessFile ("D:\\atguigu\\001.txt" , "rw");
        FileChannel fromChannel = aFile.getChannel ();
        RandomAccessFile bFile = new RandomAccessFile ("D:\\atguigu\\002.txt" , "rw");
        FileChannel toChannel = bFile.getChannel ();

        //fromChannel中的数据传输到toChannel中区
        //从第0位置到结尾
        toChannel.transferFrom (fromChannel , 0 , fromChannel.size ());

        aFile.close ();
        bFile.close ();
        System.out.println ("over!");
    }
}
